
import {
    createStore
} from 'vuex'

const setLocalCartList = (state) => {
    const { cartList } = state
    const cartListString = JSON.stringify(cartList)
    localStorage.cartList = cartListString
}
const getLocaCartList = () => {
    try {   
        return JSON.parse(localStorage.cartList);
    } catch (e) {
        return {}
    }
}
export default createStore({
    state: {
        // 数据结构---> cartList: { shopId: { shopName: '', productList: { productId: {} } } },
        cartList: getLocaCartList(),
        addressList: [],
    },
    mutations: {
        changeCartItem(state, payload) {
            const {
                shopId,
                productId,
                item,
                num
            } = payload;
            let shopInfo = state.cartList[shopId];
            if (!shopInfo) {
                shopInfo = {
                    shopName: '',
                    productList: {}
                }
            };
            let product = shopInfo.productList[productId];
            if (!product) {
                product = item;
                product.count = 0;
            }
            if (!(num == -1 && product.count == 0)) {
                product.count += num;
            }
            if (num > 0) {
                product.check = true;
            }
            shopInfo.productList[productId] = product;
            state.cartList[shopId] = shopInfo;
            setLocalCartList(state);
            // console.log(state.cartList);
        },
        changeCartItemChecked(state, payload) {
            const {
                shopId,
                productId,
            } = payload;
            const product = state.cartList[shopId].productList[productId];
            product.check = !product.check;
            setLocalCartList(state);
        },
        cleanCartProducts(state, payload) {
            const { shopId } = payload;
            state.cartList[shopId].productList = {};
            setLocalCartList(state);
        },
        setCartItemsChecked(state, payload) {
            const { shopId } = payload;
            const products = state.cartList[shopId].productList;
            if (products) {
                for (let key in products) {
                    const product = products[key];
                    product.check = true;
                }
            }
            setLocalCartList(state);
        },
        changeShopName(state, payload) {
            const { shopId, shopName } = payload;
            const shopInfo = state.cartList[shopId] || {
                shopName: '',
                productList: {}
            }
            shopInfo.shopName = shopName;
            state.cartList[shopId] = shopInfo;
            setLocalCartList(state);
        },
        clearCartData(state, shopId) {
            state.cartList[shopId].productList = {};
            setLocalCartList(state);
        },
        changeAddressList(state, addressList) {
          state.addressList.splice(0, state.addressList.length, ...addressList)
        }
    },
    actions: {},
    modules: {}
})